<?php
namespace Main\Controller;
use Think\Controller;
class AuthController extends AppController{
    public function __construct()
    {
        parent::__construct();
        $this->AuthModel = D('Auth');
    }

    public function index()
    {
        //获取所有的管理员
        $this->Authlist = $this->AuthModel->select();
        $Authlist = $this->AuthModel->field('auth_id id,auth_name,auth_pid pid,auth_controller,auth_action')->select();
        $this->Authlist = getTree($Authlist);
        $this->display();
    }
    //添加角色
    public function add()
    {
        if(IS_POST){
            $data = $this->AuthModel->create();
            if( !$data ){
                $this->error('添加错误!');die;
            }
            $res = $this->AuthModel->add( $data );
            if($res){
                $this->success('添加成功',U('index'),2);die;
            }else{
                $this->error('添加失败,错误为:'.$this->AuthModel->getError());die;
            }
        }
        $where = "auth_pid=0";
        $this->topAuth = $this->AuthModel->getAuth($where);


        $this->display();
    }
    //编辑角色
    public function edit()
    {
        $id = I('get.auth_id');
        $info = $this->AuthModel->select($id);
        $this->assign('info',$info);
        if(IS_POST){
            $data['auth_id'] = I('post.auth_id');
            $data['auth_name'] = trim(I('auth_name'));
            $data['is_menu'] = I('is_menu');
            $data['auth_controller'] = trim(I('auth_controller'));
            $data['auth_action'] = trim(I('auth_action'));
            $res = $this->AuthModel->save( $data );
            // var_dump($data);exit;
            $res?$this->success('修改成功',U('index'),1):$this->error('修改失败');
        }else{
            $this->display();
        }

    }
    //删除角色
    public function del()
    {
        $id = I('get.auth_id');
        $res = $this->AuthModel->delete($id);
        $res?$this->success('删除成功',U('index'),1):$this->error('删除失败');
    }
}